<template>
	<view class="main-body flex flex-col">
		<div class="overflow-auto flex-auto h-10">
			<u-form :model="form" ref="uForm">
						<!-- <div>
							dsf{{type}}
						</div> -->
						<u-form-item label="隐患地点" label-width="labelWidth" required>
							<u-input v-model="form.position" v-if="type=='edit'" />
							<template v-else>
								{{form.position}}
							</template>
						</u-form-item>
			
						<u-form-item label="隐患类型" label-width="labelWidth" required>
							<!-- <u-input v-model="form.danger_cate" /> -->
							<template v-if="type=='edit'">
								<u-input v-model="danger_cate_name" @click="cateShow = true" disabled />
								<u-select v-model="cateShow" :list="cate_list" @confirm="typeConfirm"></u-select>
							</template>
							<template v-else>
								{{form.danger_cate}}
							</template>
			
						</u-form-item>
						<u-form-item label="隐患负责人" label-width="labelWidth" required>
							<template v-if="type=='edit'">
								<u-input v-model="charge_user" disabled />
							</template>
							<template v-else>
								{{form.charge_user_id}}
							</template>
						</u-form-item>
						<u-form-item label="隐患描述" label-width="labelWidth" required>
							<u-input v-model="form.danger_detail" v-if="type=='edit'" />
							<template v-else>
								{{form.danger_detail}}
							</template>
						</u-form-item>
						<u-form-item label="是否已处理" label-width="labelWidth" required v-if="type=='edit'">
							<u-switch v-model="isProcess" active-value="1" inactive-value="0" @change="process"></u-switch>
						</u-form-item>
						<!-- <u-form-item label="隐患类型">
							<u-input v-model="form.type" type="select" @click="typeShow = true" />
							<u-select v-model="typeShow" :list="typeList" @confirm="confirm"></u-select>
						</u-form-item> -->
						<u-form-item label="上传图片" label-width="labelWidth" required>
							<u-upload :action="action" ref="uUpload" :max-count="4"   v-if="type=='edit'">
							</u-upload>
							<u-upload :action="action" ref="uUpload" :max-count="4" :deletable="type=='edit'?true:false"   :custom-btn="true" v-else>
								
							</u-upload>
						</u-form-item>
						<u-form-item label="上传视频" label-width="labelWidth" required v-if="form.resources_video||type=='edit'">
							<!-- <div>{{'http://safe.81game.top'+form.resources_video}}</div> -->
							<div class="up-btn" @click="upVideo" v-if="type=='edit'">
								<div class="text">上传视频</div>
							</div>
							<div class="upload-main">
								<video v-if="form.resources_video" :src="form.resources_video" controls></video>
							</div>
						</u-form-item>
						<u-button @click="submit" type="primary" v-if="type=='edit'">提交</u-button>
					</u-form>
		</div>		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		getCateList,
		upDanger,
		getDetail
	} from "@/api/hidden";
	let base_url = 'https://safe.81game.top'
	export default {
		data() {
			return {
				// base_url: 'https://safe.81game.top',
				action: base_url + '/api/file/upload_img.html',
				labelWidth: 150,
				form: {
					user_id: uni.getStorageSync('userInfo').id,
					org_id: uni.getStorageSync('userInfo').org_id,
					user_name: uni.getStorageSync('userInfo').nickname,
					position: '',
					danger_detail: '',
					danger_cate: 1,
					charge_user_id: '',
					resources: [],
					resources_video: '',
					danger_status: 0,
				},
				danger_id: 0,
				isProcess: false,
				charge_user: '',
				cate_list: [],
				danger_cate_name: '',
				cateShow: false,
				typeShow: false,
				type: 'view',
				imgLists: [],
				showUploadList: false,
				dangerflow: [],
			}
		},
		onLoad(obj) {
			console.log(11, obj)
			this.type = obj.type
			if (this.type == 'edit') {
				this.getCateList()
				this.type = obj.type ? obj.type : 'view'
				this.danger_id = obj.id
				// this.getDetail()
			} else {
				this.danger_id = obj.id
				this.getDetail()
			}

		},
		methods: {
			typeConfirm(e) {
				let index = e[0].value - 1;
				this.form.danger_cate = e[0].value
				this.form.charge_user_id = this.cate_list[index].charge_user_id
				this.charge_user = this.cate_list[index].charge_user
				this.danger_cate_name = e[0].label
			},
			getDetail() {
				getDetail({
					user_id: uni.getStorageSync('userInfo').id,
					org_id: uni.getStorageSync('userInfo').org_id,
					danger_id: this.danger_id
				}).then(res => {
					console.log(666, res)
					this.form = res.data.data.dangerinfo
					// this.imgLists = res.data.data.dangerinfo.up_imgs
					// this.imgLists = this.$refs.uUpload.lists;
					this.dangerflow = res.data.data.dangerflow
					let temp = []
					res.data.data.dangerinfo.up_imgs.forEach((item, index) => {
						temp.push({
							url: item.res_url
						})
						console.log(1, item)
					})
					this.$refs.uUpload.lists = temp
					this.form.resources_video = res.data.data.dangerinfo.up_video
				})
			},
			getCateList() {
				getCateList({
					org_id: uni.getStorageSync('userInfo').org_id,
				}).then(res => {
					console.log(11555, res.data.data)
					let list = []
					res.data.data.forEach(function(value, index, array) {
						list.push({
							"value": value.cate_id,
							"label": value.cate_name,
							"charge_user_id": value.charge_user_id,
							"charge_user": value.charge_user,
						})
					})
					this.cate_list = list
				})
			},
			upVideo() {
				var self = this;
				uni.chooseVideo({
					count: 1,
					sourceType: ['camera', 'album'],
					success: function(res) {

						uni.showLoading({
							title: '正在上传视频...'
						});
						self.videoFile = res.tempFilePath;
						uni.uploadFile({
							url: base_url + '/api/file/upload_video.html',
							// url: 'http://safe.cqjhhr.com/api/file/upload_video',
							filePath: res.tempFilePath,
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: (uploadFileRes) => {
								let ute = JSON.parse(uploadFileRes.data)
								self.form.resources_video = ute.data.src
								uni.hideLoading()
							}
						});
					}
				});
			},
			submit() {
				this.getFileList()
				upDanger(this.form).then(res => {
					console.log(1, res)
					if (res.data.status == "1") {
						this.$refs.uToast.show({
							title: '提交成功',
							type: 'success',
							url: '/pages/hidden/hidden'
						})
					} else {
						this.$refs.uToast.show({
							title: '提交失败!' + res.data.msg,
							type: 'error'
						})
					}
				}, err => {
					console.log(2, err)
				})
				// console.log(22, this.form,uni.getStorageSync('id'))
			},
			// 是否已处理
			process(val) {
				console.log(1, val)
				this.form.danger_status = val
			},
			getFileList() {
				let files = [];
				files = this.$refs.uUpload.lists.filter(val => {
					return val.progress == 100;
				})
				let newList = this.makeFileList(files)

				this.form.resources = newList
			},
			// [{'res_url':'/Uploads/2222/231.png','res_type':1,'res_ext':'png'}] 
			makeFileList(files) {
				let list = []
				files.forEach(function(value, index, array) {
					// list.push(value.response.data)
					list.push({
						"res_url": value.response.data.src,
						'res_type': 1,
						'res_ext': value.response.data.src.substr(value.response.data.src.lastIndexOf(
							".") + 1)
					})
				});
				return list
			}
		}
	}
</script>

<style lang="scss" scoped>
	// .main-body {
	// 	padding: 20rpx 40rpx;
	// }
	@import './hidden.scss'
</style>
